var payHours = 1;
var cph = '',openid='';
var perMonthMoney = 0;
var ableStatus = false;
$(function(){
	openid = $('#openid').val();
	if(openid!=null&&openid!=''){
		localStorage.setItem("openid",openid);
	}else{
		openid = localStorage.getItem("openid");
	}
	if(openid==''||openid==null){
		// location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx3a8350d9178db579&redirect_uri=https://h5.csnits.com/park/redirect2&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect';
		layer.open({
			content:'请重新扫码进入'
			,skin: 'msg'
			,time: 2 //2秒后自动关闭
		});
	}
	$.ajax({
		url: '/park/data/getUserInfo?openid='+openid+'&type=1',
		type: 'post',
		dataType: 'json',
		success: function(res) {
			if(res.data!=null){
				cph = res.data.cph;
				$.ajax({
					url:'/park/data/getByInfo',
					data:'carLicense='+cph+'&openid='+openid+'&type=1',
					type:'post',
					dataType:'json',
					success:function(res){
						streetNo = res.data.blocks.monthlyPackageMsg.rows[0][2];
						if(res.data.blocks.monthlyPackageMsg.rows[0][1]!=null&&res.data.blocks.monthlyPackageMsg.rows[0][1]!=''){
							perMonthMoney = parseFloat(res.data.blocks.monthlyPackageMsg.rows[0][1]);
							ableStatus = true;
						}
						$('#company').text(res.data.blocks.monthlyPackageMsg.rows[0][0]);
						$('#streetName').text(res.data.blocks.monthlyPackageMsg.rows[0][2]);
						$('#name').text(res.data.blocks.monthlyPackageMsg.rows[0][3]);
						$('#cph').text(res.data.blocks.monthlyPackageMsg.rows[0][6]);
						$('#endTime').text(res.data.blocks.monthlyPackageMsg.rows[0][5]);
					}
				})
			}
		}
	})
})
var startTime = '';
var streetNo = ''; 
var orderNo = 'BYORDER';
//加法
function _add(){
	var now = parseFloat($('#hours').text());
	now = 1+now;
	if(now>=12){
		layer.open({
		    content:'最多选择12个月'
		    ,skin: 'msg'
		    ,time: 2 //2秒后自动关闭
		  });
	}else{
	  $('#hours').text(now);
	  payHours = now;
	  _getPrice(); 
	}
}
//减法
function _sub(){
	var now = parseFloat($('#hours').text());
	now = now-1;
	if(now<0){
		 layer.open({
		    content:'再减就没了'
		    ,skin: 'msg'
		    ,time: 2 //2秒后自动关闭
		  });
	}else{
		$('#hours').text(now);
		payHours = now;
		 _getPrice();
	}
}
//计算金钱
function _getPrice(){
	console.log(ableStatus+'--'+perMonthMoney)
	if(!ableStatus){
		 layer.open({
		    content:'该场库暂不支持线上包月续费'
		    ,skin: 'msg'
		    ,time: 2 //2秒后自动关闭
		  });
	}else{
		$('#amount').html(parseFloat(perMonthMoney*$('#hours').text()).toFixed(2));
	}
}

function _pay(){
	if(!ableStatus){
		 layer.open({
		    content:'该场库暂不支持线上包月续费'
		    ,skin: 'msg'
		    ,time: 2 //2秒后自动关闭
		  });
	}else{
		var amount = $('#amount').text();//;
	var payMode = 'WECHAT';
	if(parseFloat(amount)<=0.00){
		 layer.open({
		     content: '暂不需要支付'
		    ,skin: 'msg'
		    ,time: 2 //2秒后自动关闭
		});
	}else{
		$.ajax({
			url:'/park/data/getPayInfo',
			type:'post',
			data:'openid='+openid+'&amount='+parseFloat(amount)*100+'&payMode='+payMode+'&orderNo='+orderNo+'&streetNo='+streetNo+'&_type=GZ&cph='+cph+'&_month='+$('#hours').text(),
			dataType:'json',
			success:function(res){
				localStorage.setItem("order_no",orderNo);
				if('GLOBAL_SUCCESS'==res.data.cmdRetCode){
					d = res.data.respData;
					callpay();
				}else{
					 layer.open({
					     content: res.data.retMsg
					    ,skin: 'msg'
					    ,time: 2 //2秒后自动关闭
					});
				}
			}
		})
	}
	}
}
var d = {};
function onBridgeReady(){
   WeixinJSBridge.invoke(
      'getBrandWCPayRequest', {
         "appId":d.appId,     //公众号ID，由商户传入     
         "timeStamp":d.timeStamp,         //时间戳，自1970年以来的秒数     
         "nonceStr":d.nonceStr, //随机串     
         "package":d.package,     
         "signType":d.signType,         //微信签名方式：     
         "paySign":d.paySign //微信签名 
      },
      function(res){
    	  WeixinJSBridge.log(res.err_msg);
	      if(res.err_msg == "get_brand_wcpay_request:ok" ){
	    	  layer.open({
	    		    content: res
	    		    ,btn: '好的，么么哒',
	    		    function(){
	    		    	 location.href = '/park/byxq'; 
	    		    }
	    		  }); 
	     }else{
	    	  layer.open({
    		    content: '支付没有成功！'
    		    ,btn: '好的，了解'
    		  }); 
	      } 
   }); 
}

function callpay(){
	if (typeof WeixinJSBridge == "undefined") {
	    if (document.addEventListener) {
	        document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
	    } else if (document.attachEvent) {
	        document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
	        document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
	    }
	} else {
	    onBridgeReady();
	}
}